unit MainForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ImgList;

type
  TfmCueDemo = class(TForm)
    Panel1: TPanel;
    edTitle: TEdit;
    Label1: TLabel;
    edPerformer: TEdit;
    Label2: TLabel;
    edSongwriter: TEdit;
    Label3: TLabel;
    edCDT: TEdit;
    Label4: TLabel;
    edCat: TEdit;
    Label5: TLabel;
    StatusBar1: TStatusBar;
    TreeView1: TTreeView;
    Button1: TButton;
    ImageList1: TImageList;
    edGenre: TEdit;
    Label6: TLabel;
    edDiscID: TEdit;
    Label7: TLabel;
    Label8: TLabel;
    edDate: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  fmCueDemo: TfmCueDemo;

implementation

{$R *.dfm}

uses libcue;

var cue: pointer;

function IsEmpty(pws: PWideChar): boolean;
begin
  Result := true;
  if pws = nil then exit;
  Result := WideString(pws) = WideString('')
end;

procedure TfmCueDemo.Button1Click(Sender: TObject);
var ws: PWideChar;
    i,j,k: integer;
    en,trk: TTreeNode;
    _en,_trk: pointer;
    einfo: TCUEEntryInfo;
    tinfo: TCUETrackInfo;
    idx: TCUETrackIndex;
begin
  TreeView1.Items.Clear;
  with TOpenDialog.Create(nil) do begin
    Filter := 'CUE files|*.cue';
    if Execute then
  if CUE_Load(cue, PWideChar(WideString(Filename))) then begin
    // Load global info
    if CUE_GlobalInfo(cue, CUE_INFO_TITLE, ws) then
      edTitle.Text := ws else edTitle.Text := '';
    if CUE_GlobalInfo(cue, CUE_INFO_PERFORMER, ws) then
      edPerformer.Text := ws else edPerformer.Text := '';
    if CUE_GlobalInfo(cue, CUE_INFO_SONGWRITER, ws) then
      edSongwriter.Text := ws else edSongwriter.Text := '';
    if CUE_GlobalInfo(cue, CUE_INFO_CATALOG, ws) then
      edCat.Text := ws else edCat.Text := '';
    if CUE_GlobalInfo(cue, CUE_INFO_CDTEXT, ws) then
      edCDT.Text := ws else edCDT.Text := '';
    if CUE_GlobalInfo(cue, CUE_INFO_DISCID, ws) then
      edDiscid.Text := ws else edDiscid.Text := '';
    if CUE_GlobalInfo(cue, CUE_INFO_GENRE, ws) then
      edGenre.Text := ws else edGenre.Text := '';
    if CUE_GlobalInfo(cue, CUE_INFO_DATE, ws) then
      edDate.Text := ws else edDate.Text := '';

    // Load files&tracks
    for i := 0 to CUE_EntryCount(cue)-1 do begin
      _en := CUE_GetEntry(cue,i);
      CUE_GetEntryInfo(_en, einfo);
      en := TreeView1.Items.Add(nil, einfo.Filename);
      en.ImageIndex := einfo.EntryClass;
      en.SelectedIndex := einfo.EntryClass;

      for j := 0 to einfo.Tracks-1 do begin
        _trk := CUE_GetEntryTrack(_en, j);
        CUE_GetTrackInfo(_trk, tinfo);

        trk := TreeView1.Items.AddChild(en, 'Track '+IntToStr(tinfo.Index));
        trk.ImageIndex    := 5+tinfo.TrackClass;
        trk.SelectedIndex := 5+tinfo.TrackClass;

        if not IsEmpty(tinfo.Title) then
          with TreeView1.Items.AddChild(trk, 'Title: '+TInfo.Title) do begin
            ImageIndex := 7;
            SelectedIndex := 7;
          end;
        if not IsEmpty(tinfo.Performer) then
          with TreeView1.Items.AddChild(trk, 'Performer: '+TInfo.Performer) do begin
            ImageIndex := 7;
            SelectedIndex := 7;
          end;
        if not IsEmpty(tinfo.Songwriter) then
          with TreeView1.Items.AddChild(trk, 'Songwriter: '+TInfo.Songwriter) do begin
            ImageIndex := 7;
            SelectedIndex := 7;
          end;
        if tinfo.ISRC <> '' then
          with TreeView1.Items.AddChild(trk, 'ISRC: '+TInfo.ISRC) do begin
            ImageIndex := 7;
            SelectedIndex := 7;
          end;

        if MSFToFrames(tinfo.Pregap) > 0 then
          with TreeView1.Items.AddChild(trk, 'Pregap: '+MSFToStr(TInfo.Pregap)) do begin
            ImageIndex := 10;
            SelectedIndex := 10;
          end;

        // indexes
        for k := 0 to tinfo.IndexCount-1 do begin
          CUE_GetTrackIndex(_trk, k, true, idx);
          with TreeView1.Items.AddChild(trk, Format('Index %.2d %s',[idx.Index, MSFToStr(idx.Start)])) do begin
            ImageIndex := 8;
            SelectedIndex := 8;
          end;
        end;

        if MSFToFrames(tinfo.Postgap) > 0 then
          with TreeView1.Items.AddChild(trk, 'Postgap: '+MSFToStr(TInfo.Postgap)) do begin
            ImageIndex := 11;
            SelectedIndex := 11;
          end;
        trk.Expand(true);
      end;
    end;
  end;
    Free;
  end;
end;

procedure TfmCueDemo.FormCreate(Sender: TObject);
begin
  cue := CUE_Init;
end;

procedure TfmCueDemo.FormDestroy(Sender: TObject);
begin
  cue_release(cue);
end;

end.
